package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.FriendCircle;
import cn.wolfcode.trip.base.domain.Integral;
import cn.wolfcode.trip.base.mapper.FriendCircleMapper;
import cn.wolfcode.trip.base.query.CommentQueryObject;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ICommentService;
import cn.wolfcode.trip.base.service.IFriendCircleService;
import cn.wolfcode.trip.base.vo.CommentTreeVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FriendCircleServiceImpl implements IFriendCircleService {

    @Autowired
    private FriendCircleMapper friendCircleMapper;
    @Autowired
    private ICommentService commentService;
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<FriendCircle> selectByUserIds(List<Long> ids, QueryObject qo) {
        //分页插件
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"f.creat_time DESC");
        List<FriendCircle> list = friendCircleMapper.selectByUserIds(ids);
        for (FriendCircle friendCircle : list) {
            CommentQueryObject comment = new CommentQueryObject();
            comment.setCommentId(friendCircle.getId());
            comment.setType(2);
            comment.setSort("ch.createTime");
            List<CommentTreeVo> commentList = commentService.getCommentByUserIdAndType(comment);
            friendCircle.setCommentTreeVos(commentList.get(0).getChildren());
        }
        return new PageInfo<>(list);
    }

    @Override
    public void save(FriendCircle fc) {
        //设置创建时间
        fc.setCreatTime(new Date());
        friendCircleMapper.insert(fc);
    }

    @Override
    public List<String> selectAllUserImgsByFriendCircleId(Long id) {
        //获取该动态的所有点赞用户
        List<Integer> users = friendCircleMapper.selectAllUserById(id);
        //获取用户所有头像
        List<String> imgs = userMapper.selectImgsByIds(users);
        return imgs;
    }

    @Override
    public PageInfo<FriendCircle> selectByUserId(Long userId, QueryObject qo) {
        //分页插件
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"f.creat_time DESC");
        //获取该用户自己的所有朋友圈
        List<FriendCircle> list = friendCircleMapper.selectUserById(userId);
        for (FriendCircle friendCircle : list) {
            CommentQueryObject comment = new CommentQueryObject();
            comment.setCommentId(friendCircle.getId());
            comment.setType(2);
            comment.setSort("ch.createTime");
            List<CommentTreeVo> commentList = commentService.getCommentByUserIdAndType(comment);
            friendCircle.setCommentTreeVos(commentList.get(0).getChildren());
        }
        return new PageInfo<>(list);
    }

}
